﻿using System.Net.Mail;
using System.Threading;
using System.IO;

namespace FamaWebSite.Notifiers
{
    public class UserNotifier : IUserNotifier
    {
        private static string _destinatario;

        public void SendNotification(string destinatarioEmail)
        {
            _destinatario = destinatarioEmail;

            // Create a new FileSystemWatcher and set its properties.
            var watcher = new FileSystemWatcher
                {
                    Path = @"C:\Users\Joilson\Desktop\FAMA\trunk\outputs",
                    Filter = "corpusGravado.txt",
                };

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnChanged);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
        }

        // Define the event handlers. 
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            Thread.Sleep(2000);

            //TODO: Coloquei esse delete aqui para fazer testes mais rápidos... ideal talvez transferir para uma pasta de enviados..
            string path = @"C:\Users\Joilson\Desktop\FAMA\trunk\outputs\corpusGravado.txt";
            File.Delete(path);

            // Send an email when the file is changed.
            var message = new MailMessage(
                to:_destinatario,
                from:"fama.framework@gmail.com",
                subject:"Teste do FAMA",
                body:"Este eh o corpo do email!");

            //message.Attachments.Add(new Attachment(@"C:\Users\Joilson\Desktop\FAMA\trunk\Joilson\result.txt"));

            var client = new SmtpClient();
            client.Send(message);

            ((FileSystemWatcher)source).Dispose();
        }
    }
}